package com.yaozheng.vocationaltraining.service.impl;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import android.graphics.Bitmap;
import android.media.Image;
import android.media.ThumbnailUtils;

import com.squareup.okhttp.RequestBody;
import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IMyUserInfoView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.service.MyUserInfoService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

/**
 * 用户资料服务
 *
 * @author Administrator
 */
@EBean
public class MyUserInfoServiceImpl implements MyUserInfoService {
    IMyUserInfoView iMyUserInfoView;

    @Override
    public void init(IMyUserInfoView iMyUserInfoView) {
        this.iMyUserInfoView = iMyUserInfoView;
    }

    @Background
    @Override
    public void getUserInfo() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/user/info",
                    iMyUserInfoView.getToken(), iMyUserInfoView);
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iMyUserInfoView.isResponseResult()) {
                iMyUserInfoView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }


    @Background
    @Override
    public void uploadAvatar(String avatarPath) {
        try {
            byte[] uploadContent = ImageUtils.bitmap2Bytes(ImageUtils
                    .getSmallBitmap(avatarPath, 300, 300));
            if (uploadContent == null) {
                iMyUserInfoView.uploadAvatarError("上传图片失败！！！");
            }
            OkHttpClientManager.getInstance().upload(
                    Constants.ROOTURL + "/user/avatar",
                    iMyUserInfoView.getToken(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iMyUserInfoView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iMyUserInfoView.uploadAvatarSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iMyUserInfoView.uploadAvatarError(error);

                        }

                        @Override
                        public boolean isResponseResult() {
                            return iMyUserInfoView.isResponseResult();
                        }

                    }), uploadContent);

        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iMyUserInfoView.isResponseResult()) {
                iMyUserInfoView
                        .uploadAvatarError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }

    }
}
